<template>
  <div class="box">
    <el-form :model="form" ref="ruleFormRef" :rules="rules" label-width="120px">

      <el-form-item label="商品名称" prop="cateName">
        <el-input v-model="form.cateName" />
      </el-form-item>

      <el-form-item label="上级分类" prop="cateId">
        <el-cascader
          v-model="form.cateId"
          :options="cates"
          :props="{
            checkStrictly: true,
            emitPath: false,
            label: 'cateName',
            value: 'id',
          }"
          clearable
        />
      </el-form-item>

      <el-form-item label="商品关键字" prop="keyword">
        <el-input v-model="form.keyword" />
      </el-form-item>

      <el-form-item label="商品描述" prop="desc">
        <el-input v-model="form.desc" type="textarea" />
      </el-form-item>

      <el-form-item label="分类图片" prop="cateIcon">
        <div class="bis">
          <el-upload
            ref="uploadRef"
            action="/api/upIoad"
            class="avatar-uploader"
            :show-file-list="false"
            name='file'
            accept=".jpg, .png, .gif"
            :data="{a:1,b:2}"
            :on-success="handleAvatarSuccess"
            :before-upload="beforeAvatarUpload"
          >
            <img v-if="form.cateIcon" :src="form.cateIcon" class="avatar" />
            <el-icon v-else class="avatar-uploader-icon"><Plus /></el-icon>
          </el-upload>
        </div>
      </el-form-item>

      <el-form-item label="商品详情">
        <div style="border: 1px solid #ccc;">
            <Toolbar
                style="border-bottom: 1px solid #ccc"
                :editor="editor"
                :defaultConfig="toolbarConfig"
                :mode="mode"
            />
            <Editor
                style="height: 500px; overflow-y: hidden;"
                v-model="form.content"
                :defaultConfig="editorConfig"
                :mode="mode"
                @onCreated="onCreated"
            />
        </div>
      </el-form-item>

      <el-form-item label="商品价格">
         <el-input-number v-model="form.price" :min="0" />
      </el-form-item>

      <el-form-item label="商品状态">
        <el-radio-group v-model="form.enabl">
          <el-radio :label="true">上架</el-radio>
          <el-radio :label="false">下架</el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item label="热卖单品">
        <el-radio-group v-model="form.best">
          <el-radio :label="true">开启</el-radio>
          <el-radio :label="false">关闭</el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item label="是否喜欢">
        <el-radio-group v-model="form.like">
          <el-radio :label="true">开启</el-radio>
          <el-radio :label="false">关闭</el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item label="精品推荐">
        <el-radio-group v-model="form.recommend">
          <el-radio :label="true">开启</el-radio>
          <el-radio :label="false">关闭</el-radio>
        </el-radio-group>
      </el-form-item>

      <el-form-item>
        <el-button type="primary" @click="onSubmit">保存</el-button>
        <el-button type="primary" @click="reset">重置</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
import { fatchCates } from '@a/cateLists'
import { updateItem, getItemDetail } from '@a/itemUpdate'
import '@wangeditor/editor/dist/css/style.css' // 引入 css
import { Editor, Toolbar } from '@wangeditor/editor-for-vue'
export default {
  name: 'ItemAdd',
  data () {
    return {
      editor: null,
      toolbarConfig: { },
      editorConfig: {
        placeholder: '请输入内容...',
        MENU_CONF: {
          uploadImage: {
            server: '/api/upIoad',
            fieldName: 'file',
            maxFileSize: 5 * 1024 * 1024,
            // maxNumberOfFiles: 5,
            timeout: 5 * 1000,
            meta: {
              token: 'xxx',
              otherKey: 'yyy'
            },
            customInsert: (res, insertFn) => {
              // console.log(res)
              if (res.code === 200) {
                insertFn(res.data.url)
              }
            }
          }
        }
      },
      mode: 'default', // or 'simple'
      rules: {
        cateName: {
          required: true,
          message: '商品名称必填'
        },
        cateId: {
          required: true,
          message: '商品分类必填'
        },
        desc: {
          required: true,
          message: '商品简介必填'
        },
        keyword: {
          required: true,
          message: '关键字必填'
        },
        cateIcon: {
          required: true,
          message: '商品封面图必须上传'
        }
      },
      form: {
        cateName: '', // 商品名称
        cateId: 1, // 商品分类
        enabl: true, // 商品状态
        best: false, // 是否热卖
        like: false, // 是否喜欢
        recommend: false, // 精品推荐
        desc: '', // 商品描述
        keyword: '', // 关键字
        cateIcon: '', // 商品封面图
        price: 0, // 商品价格
        content: '' // 商品详情
      },
      cates: []
    }
  },
  methods: {
    onCreated (editor) {
      this.editor = Object.seal(editor) // 一定要用 Object.seal() ，否则会报错
    },
    getItemDetail () {
      getItemDetail({
        id: this.$route.query.id
      }).then(res => {
        // console.log(res)
        if (res.data.code === 200) {
          this.form = res.data.data
          // console.log(res.data.data)
        }
      })
    },
    onSubmit () {
      // 保存
      this.$refs.ruleFormRef.validate().then(res => {
        updateItem(this.form).then(res => {
          // console.log(res)
          if (res.data.code === 200) {
            this.$message({
              type: 'success',
              duration: 1500,
              message: res.data.msg,
              onClose: () => {
                this.$router.replace('/itemLists')
              }
            })
          }
        })
      }).catch(() => {
        this.$message({
          type: 'error',
          message: '字段没有填写完整'
        })
      })
    },
    reset () {
      // 重置
      this.$refs.ruleFormRef.resetFields()
    },
    fatchCates () {
      fatchCates().then(res => {
        // console.log(res)
        if (res.data.code === 200) {
          this.cates = res.data.data.lists
        }
      })
    },
    handleAvatarSuccess (res) {
      // 图片上传
      this.form.cateIcon = res.data.url
      // console.log(res)
      if (res.code === 200) {
        this.$message({
          type: 'success',
          message: '上传图片成功'
        })
      }
    },
    beforeAvatarUpload (file) {
      // 图片上传之前
      // console.log(file)
      if (file.size > 1024 * 1024 * 1) {
        this.$message({
          type: 'error',
          message: '上传的图片不能超过1MB'
        })
        return false
      } else {
        return true
      }
    }
  },
  created () {
    this.fatchCates()
    this.getItemDetail()
  },
  components: {
    Editor,
    Toolbar
  }
}
</script>

<style lang="scss" scoped>
.box {
  border: 1px solid #ddd;
  padding: 20px;
  width: 60%;
}
.bis {
  border: 1px dashed #dcdfe6;
}
.avatar-uploader .avatar {
  width: 178px;
  height: 178px;
  display: block;
}
.avatar-uploader .el-upload {
  border: 1px dashed;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
  transition: var(--el-transition-duration-fast);
}

.avatar-uploader .el-upload:hover {
  border-color: var(--el-color-primary);
}
.el-icon.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 178px;
  height: 178px;
  text-align: center;
}
</style>
